from typing import *


class Solution:
    def incremovableSubarrayCount(self, nums: List[int]) -> int:
        n = len(nums)
        r = n - 1
        while r - 1 >= 0 and nums[r - 1] < nums[r]:
            r -= 1
        ans = 0
        for i in range(n):
            while r < n and i - 1 >= 0 and nums[r] <= nums[i - 1]:
                r += 1
            ans += n - r + (i != r)
            if i - 1 >= 0 and nums[i] <= nums[i - 1]:
                break
        return ans


class Solution:
    def incremovableSubarrayCount(self, nums: List[int]) -> int:
        n = len(nums)
        r = n - 1
        while r - 1 >= 0 and nums[r - 1] < nums[r]:
            r -= 1
        ans = 0
        i = 0
        while i == 0 or i < n and nums[i] > nums[i - 1]:
            while r < n and nums[r] <= nums[i]:
                r += 1
            ans += n - r
            i += 1
        return ans


s = Solution()
print(
    ans := s.incremovableSubarrayCount([1, 2, 3, 4, 5, 6, 7, 8, 4, 5, 6, 7, 8, 9, 10]),
    ans == 57,
)
print(ans := s.incremovableSubarrayCount([1, 2, 3, 4]), ans == 10)
print(ans := s.incremovableSubarrayCount([6, 5, 7, 8]), ans == 7)


# class Solution:
#     def incremovableSubarrayCount(self, nums: List[int]) -> int:
#         n = len(nums)
#         ans = 0
#         h = [0 for _ in range(n)]
#         for i in range(n):
#             for j in range(i, n):
#                 l, t, k = -1, True, 0
#                 while k < n and t:
#                     if k < i or k > j:
#                         if nums[k] > l:
#                             l = nums[k]
#                         else:
#                             t = False
#                     k += 1
#                 if t:
#                     h[i] += 1
#                     print(i, j)
#                     ans += 1
#         print(h)
#         return ans


# s = Solution()
# print(s.incremovableSubarrayCount([1, 2, 3, 4, 5, 6, 7, 8, 4, 5, 6, 7, 8, 9, 10]))
